#include<vector>
#include<unordered_set>
#include<algorithm>
using namespace std;

class Solution {
    int encode(int x,int y) {
        return 40001 * x + y;
    }

    int area(const vector<int>&p1, const vector<int>& p2) {
        return abs((p1[0] - p2[0])  * (p1[1] - p2[1]));
    }

public:
    int minAreaRect(vector<vector<int>>& points) {
        unordered_set<int>set;
        for (int i = 0; i < points.size(); ++i) {
            set.emplace(encode(points[i][0], points[i][1]));
        }
        int ans = INT32_MAX;
        for (int i = 0; i < points.size(); ++i) {
            for (int j = i+1; j < points.size(); ++j) {
                if (points[i][0] == points[j][0] || points[i][1] == points[j][1])continue;
                if (set.find(encode(points[i][0], points[j][1])) != set.end() &&
                    set.find(encode(points[j][0], points[i][1])) != set.end()) {
                    ans = min(ans, area(points[i], points[j]));
                }
            }
        }
        if (ans == INT32_MAX)
            ans = 0;
        return ans;
    }
};